import { fromJS } from 'immutable'
import { actionType } from './index'

// 将对象转换为immutable对象，immutable意为不可修改
const defaultState = fromJS({
  topicList: [],
  articleList: [],
  writerList: [],
  recommendList: [],
  page: 1,
  showScroll: false
})
const changeShowBack = (state, action) => {
  return state.set('showScroll', action.data)
}
const setHomeList = (state,action) => {
  let page = state.get('page')
  let { articleList, topicList, recommendList } = action.data
  return state.merge({
    articleList: fromJS(articleList),
    topicList: fromJS(topicList),
    recommendList: fromJS(recommendList),
  }).set('page', page += 1)
}
const reducer = (state = defaultState, action) => {
  switch (action.type) {
    case actionType.SET_HOME_LIST:
      return setHomeList(state, action)
    case actionType.CHANGE_SHOW_BACK:
      return changeShowBack(state, action)
    default:
      return state
  }
}

export default reducer